package ua.kratik.seabattle.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import ua.kratik.seabattle.dao.BasicDAO;
import ua.kratik.seabattle.dao.DataObject;

public abstract class GenericDataService<D extends DataObject> 
				implements BasicDataService<D> {

	private Logger logger = LoggerFactory.getLogger(GenericDataService.class);
	
	private BasicDAO<D> genericDAO;
	
	public GenericDataService(BasicDAO<D> dao){
		if (dao != null){
			this.genericDAO = dao;
			logger.debug("Initialization class ["+ this.getClass().toString() + "] successful");
		} else{
			logger.error("Initialization class ["+ this.getClass().toString() + "] faild: DAO cannot be null!!!!!!");
			throw new NullPointerException("DAO cannot be null!!!!!");
		}
	}
	
	@Transactional
	public void add(D dto) {
		genericDAO.add(dto);
	}

	@Transactional
	public void addAll(List<D> dtos) {
		genericDAO.addAll(dtos);
	}

	@Transactional
	public void remove(D dto) {
		genericDAO.remove(dto);
	}

	@Transactional
	public void removeById(Integer id) {
		genericDAO.removeById(id);
	}

	@Transactional
	public void removeAll(List<D> dtos) {
		genericDAO.removeAll(dtos);
	}

	@Transactional
	public D findById(Integer id) {
		return genericDAO.findById(id);
	}

	@Transactional
	public List<D> findAll() {
		return genericDAO.findAll();
	}

	@Transactional
	public void update(D dto) {
		genericDAO.update(dto);
	}

	@Transactional
	public void refresh(D dto) {
		genericDAO.refresh(dto);
	}

}
